/**
 * 
 */
package cn.ad.server.web.servlet.v2.parser;

import cn.ad.server.common.exception.DecrypterException;
import cn.ad.server.common.utils.BESDecryptUtil;
import cn.ad.server.web.servlet.v2.requests.BESImpressionRequest;
import cn.ad.server.web.servlet.v2.requests.TrackingRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * bes监测请求解析-展示
 * 
 * @author wangwei
 *
 */
public class BESImpressionRequestParser extends BESTrackingRequestParser {

	private static Logger LOGGER = LoggerFactory.getLogger(BESImpressionRequestParser.class);
	/**
	 * bes解密口令
	 */
	private String ekey;
	private String ikey;

	/**
	 * bes的ADX 是 将price封装在 展示请求中的
	 */
	public static String PARAM_PRICE = "price";

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.ad.server.web.servlet.v2.parser.
	 * GDTTrackingRequestParser#readTrackingParam(javax.servlet.http.
	 * HttpServletRequest,
	 * cn.falconnect.adserver.adapter.TrackingRequest)
	 */
	@Override
	public void readTrackingParam(HttpServletRequest request, TrackingRequest trackingRequest)
			throws AdRequestParseException {
		super.readTrackingParam(request, trackingRequest);
		try {
			String winPrice = request.getParameter(PARAM_PRICE);
			BESImpressionRequest besReq = (BESImpressionRequest) trackingRequest;
			besReq.setEncryptWinPrice(winPrice);
			readWinPrice(besReq);
		} catch (ParameterParseExcption ex) {
			ex.setResult(trackingRequest);
			throw ex;
		}
	}
	

	private long readWinPrice(HttpServletRequest request) throws ParameterParseExcption {
		String winPrice = request.getParameter(PARAM_PRICE);
		long price = 0l;
		if (StringUtils.isNoneEmpty(winPrice) && StringUtils.isNotBlank(winPrice)) {
			try {
				price = BESDecryptUtil.decrypt(winPrice, ekey, ikey);
			} catch (DecrypterException e) {
				throw new ParameterParseExcption(DefaultWinoticeRequestParser.PARAM_WINOTICE_PRICE, null,
						e.getMessage());
			}
		} else {
			throw new ParameterParseExcption(DefaultWinoticeRequestParser.PARAM_WINOTICE_PRICE, null,
					"解析BES-winprice出现异常");
		}
		return price;
	}
	
	private void readWinPrice(BESImpressionRequest req) throws ParameterParseExcption {
		String winPrice = req.getEncryptWinPrice();
		long price = 0l;
		if (StringUtils.isNoneEmpty(winPrice) && StringUtils.isNotBlank(winPrice)) {
			try {
				price = BESDecryptUtil.decrypt(winPrice, ekey, ikey);
				req.setWin_price(price);
			} catch (DecrypterException e) {
				LOGGER.error("解密BES-winprice出现异常",e);
				req.setOkDecrypt(false);
			}
		} else {
			LOGGER.error("解密BES-winprice出现异常:待解密的winprice值为空");
			req.setOkDecrypt(false);
		}
	}

	public String getEkey() {
		return ekey;
	}

	public void setEkey(String ekey) {
		this.ekey = ekey;
	}

	public String getIkey() {
		return ikey;
	}

	public void setIkey(String ikey) {
		this.ikey = ikey;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.ad.server.web.servlet.v2.parser.
	 * TrackingRequestParser#createTrackingRequest(javax.servlet.http.
	 * HttpServletRequest)
	 */
	@Override
	public TrackingRequest createTrackingRequest(HttpServletRequest request) throws AdRequestParseException {
		return new BESImpressionRequest();
	}

}
